﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeltaPlcSystem
{
    public partial class EnableForm : Form
    {
        public delegate void SaveDele(Dictionary<string, bool> saveDataDic,int mode);
        public event SaveDele SaveEvent;
        int mode = 0;
        public EnableForm(List<DeviceClass> deviceList,int model)
        {
            InitializeComponent();

            MaximizeBox = false;

            mode = model;
            if (model == 0)
            {
                uiButton1.Text = "保存";
                Text = "启用窗口";

                if (deviceList.Count > 0)
                {
                    for (int i = 0; i < 50; i++)
                    {
                        if(deviceList.Count<=i*3)
                        {
                            this.Height = 60 * i + 70 + uiButton1.Height;
                            uiButton1.Location = new Point(this.Width - 220, this.Height - 130);                          
                            break;
                        }
                    }


                    for (int i = 0; i < deviceList.Count; i++)
                    {
                        if (i % 3 == 0)
                        {
                            UICheckBox ucb = new UICheckBox();
                            ucb.BackColor = Color.Transparent;
                            ucb.Location = new Point((Width - 3 * ucb.Width) / 4, 20 + 20 * i / 3 + ucb.Height * i / 3);
                            if (deviceList[i].IsEnable == "1")
                            {
                                ucb.Checked = true;
                            }
                            else
                                ucb.Checked = false;
                            ucb.Text = deviceList[i].Ip;
                            uiPanel1.Controls.Add(ucb);
                        }
                        else if (i == 1 || i == 4 || i == 7 || i == 10 || i == 13 || i == 16 || i == 19 || i == 22 || i == 25 || i == 28) 
                        {
                            UICheckBox ucb = new UICheckBox();
                            ucb.BackColor = Color.Transparent;
                            ucb.Location = new Point((Width - 3 * ucb.Width) / 2 + ucb.Width, 20 + 20 * (i - 1) / 3 + ucb.Height * (i - 1) / 3);
                            if (deviceList[i].IsEnable == "1")
                            {
                                ucb.Checked = true;
                            }
                            else
                                ucb.Checked = false;
                            ucb.Text = deviceList[i].Ip;
                            uiPanel1.Controls.Add(ucb);
                        }
                        else if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14 || i == 17 || i == 20 || i == 23 || i == 26 || i == 29)
                        {
                            UICheckBox ucb = new UICheckBox();
                            ucb.BackColor = Color.Transparent;
                            ucb.Location = new Point((Width - 3 * ucb.Width) / 4 * 3 + 2 * ucb.Width, 20 + 20 * (i - 2) / 3 + ucb.Height * (i - 2) / 3);
                            if (deviceList[i].IsEnable == "1")
                            {
                                ucb.Checked = true;
                            }
                            else
                                ucb.Checked = false;
                            ucb.Text = deviceList[i].Ip;
                            uiPanel1.Controls.Add(ucb);
                        }
                    }
                }
              
            }
            else 
            { 
                uiButton1.Text = "确定";
                Text = "删除窗口";
                if (deviceList.Count > 0)
                {
                    for (int i = 0; i < 50; i++)
                    {
                        if (deviceList.Count <= i * 3)
                        {
                            this.Height = 60 * i + 70 + uiButton1.Height;
                            uiButton1.Location = new Point(this.Width - 220, this.Height - 120);
                            break;
                        }
                    }

                    for (int i = 0; i < deviceList.Count; i++)
                    {
                        if (i % 3 == 0)
                        {
                            UICheckBox ucb = new UICheckBox();
                            ucb.BackColor = Color.Transparent;
                            ucb.Location = new Point((Width - 3 * ucb.Width) / 4, 20 + 20 * i / 3 + ucb.Height * i / 3);
                         
                            ucb.Text = deviceList[i].Ip;
                            uiPanel1.Controls.Add(ucb);
                        }
                        else if (i == 1 || i == 4 || i == 7 || i == 10 || i == 13 || i == 16 || i == 19 || i == 22 || i == 25 || i == 28)
                        {
                            UICheckBox ucb = new UICheckBox();
                            ucb.BackColor = Color.Transparent;
                            ucb.Location = new Point((Width - 3 * ucb.Width) / 2 + ucb.Width, 20 + 20 * (i - 1) / 3 + ucb.Height * (i - 1) / 3);
                         
                            ucb.Text = deviceList[i].Ip;
                            uiPanel1.Controls.Add(ucb);
                        }
                        else if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14 || i == 17 || i == 20 || i == 23 || i == 26 || i == 29)
                        {
                            UICheckBox ucb = new UICheckBox();
                            ucb.BackColor = Color.Transparent;
                            ucb.Location = new Point((Width - 3 * ucb.Width) / 4 * 3 + 2 * ucb.Width, 20 + 20 * (i - 2) / 3 + ucb.Height * (i - 2) / 3);
                        
                            ucb.Text = deviceList[i].Ip;
                            uiPanel1.Controls.Add(ucb);
                        }
                    }
                }
           
            }

        
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Dictionary<string, bool> tempDic = new Dictionary<string, bool>();
            foreach (Control item in uiPanel1.Controls)
            {
                if (item is UICheckBox)
                {
                    UICheckBox uc = (UICheckBox)item;

                    tempDic.Add(uc.Text, uc.Checked);
                }
            }

            SaveEvent(tempDic, mode);

            if (mode == 0)
            {
               
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("删除成功");

                this.Close();
            }
        }
    }
}
